package cn.jetpiece.cloud.joss.app.modular.basics.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.jetpiece.cloud.joss.app.modular.basics.mapper.StorageClassMapper;
import cn.jetpiece.cloud.joss.app.modular.basics.service.StorageClassService;
import cn.jetpiece.cloud.joss.app.modular.basics.wrapper.StorageClassWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.jetpiece.cloud.joss.api.basic.model.params.CreateStorageClassParam;
import cn.jetpiece.cloud.joss.api.basic.model.params.UpdateStorageClassParam;
import cn.jetpiece.cloud.joss.api.basic.model.query.StorageClassQuery;
import cn.jetpiece.cloud.joss.api.basic.model.result.StorageClassResult;
import cn.jetpiece.cloud.joss.app.modular.basics.entity.StorageClass;
import cn.jetpiece.cloud.joss.app.modular.object.enums.BasicExceptionEnum;
import cn.jetpiece.cloud.core.page.PageFactory;
import cn.jetpiece.cloud.model.exception.ServiceException;
import cn.jetpiece.cloud.model.page.PageResult;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 存储类型 服务实现类
 * </p>
 *
 * @author XuJZ
 * @since 2020-09-05
 */
@Service
public class StorageClassServiceImpl extends ServiceImpl<StorageClassMapper, StorageClass> implements StorageClassService {

    @Override
    public boolean add(CreateStorageClassParam param) {
        LambdaQueryWrapper<StorageClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StorageClass::getProviderId, param.getProviderId());
        wrapper.eq(StorageClass::getStorageClassValue, param.getStorageClassValue());
        if (super.count(wrapper) > 0) {
            throw new ServiceException(BasicExceptionEnum.STORAGE_CLASS_REPEAT);
        }
        wrapper.clear();
        wrapper.eq(StorageClass::getProviderId, param.getProviderId());
        wrapper.eq(StorageClass::getStorageClassName, param.getStorageClassName());
        if (super.count(wrapper) > 0) {
            throw new ServiceException(BasicExceptionEnum.STORAGE_CLASS_REPEAT);
        }
        StorageClass bean = new StorageClass();
        BeanUtil.copyProperties(param, bean);
        return super.save(bean);
    }

    @Override
    public boolean update(UpdateStorageClassParam param) {
        StorageClass oldBean = super.getById(param.getId());
        if (oldBean == null) {
            throw new ServiceException(BasicExceptionEnum.STORAGE_CLASS_NOT_EXISTED);
        }
        if (StrUtil.isNotEmpty(param.getStorageClassName())) {
            LambdaQueryWrapper<StorageClass> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(StorageClass::getStorageClassValue, param.getStorageClassValue());
            wrapper.eq(StorageClass::getProviderId, oldBean.getProviderId());
            if (super.count(wrapper) > 0) {
                throw new ServiceException(BasicExceptionEnum.STORAGE_CLASS_REPEAT);
            }
        }
        if (StrUtil.isNotEmpty(param.getStorageClassValue())) {
            LambdaQueryWrapper<StorageClass> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(StorageClass::getProviderId, oldBean.getProviderId());
            wrapper.eq(StorageClass::getStorageClassName, param.getStorageClassName());
            if (super.count(wrapper) > 0) {
                throw new ServiceException(BasicExceptionEnum.STORAGE_CLASS_REPEAT);
            }
        }
        StorageClass update = BeanUtil.copyProperties(param, StorageClass.class);
        update.setId(param.getId());
        return super.updateById(update);
    }

    @Override
    public boolean delete(String id) {
        StorageClass bean = super.getById(id);
        if (bean == null) {
            throw new ServiceException(BasicExceptionEnum.STORAGE_CLASS_NOT_EXISTED);
        }
        return super.removeById(id);
    }

    @Override
    public PageResult<StorageClassResult> page(StorageClassQuery param) {
        LambdaQueryWrapper<StorageClass> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(param.getId())) {
            wrapper.eq(StorageClass::getId, param.getId());
        }
        if (StrUtil.isNotEmpty(param.getProviderId())) {
            wrapper.eq(StorageClass::getProviderId, param.getProviderId());
        }
        if (StrUtil.isNotEmpty(param.getStorageClassName())) {
            wrapper.eq(StorageClass::getStorageClassName, param.getStorageClassName());
        }
        if (StrUtil.isNotEmpty(param.getStorageClassValue())) {
            wrapper.eq(StorageClass::getStorageClassValue, param.getStorageClassValue());
        }
        if (param.getIsEnable() != null) {
            wrapper.eq(StorageClass::getIsEnable, param.getIsEnable());
        }
        IPage<StorageClass> iPage = PageFactory.defaultPage();
        IPage<StorageClass> page = super.page(iPage, wrapper);
        return new StorageClassWrapper(page).setWrapper(param.getWrapper()).wrapPageResult();
    }
}
